package com.seu.clab.farm.service.impl;

import com.seu.clab.farm.exception.BizException;
import com.seu.clab.farm.model.dao.entity.*;
import com.seu.clab.farm.model.dao.mapper.*;
import com.seu.clab.farm.service.ContractService;
import com.seu.clab.farm.model.dto.post.ContractDTO;
import com.seu.clab.farm.util.ConstantUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;


@Service
public class ContractServiceImpl implements ContractService {
    @Autowired
    ContractMapper contractMapper;

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Object getContractit(Integer flag, String accountId) {
        return contractMapper.getContract(flag, accountId);
    }

    @Override
    public Contract addContract(ContractDTO contractDTO, String accountId) {
        // log
        String adminName = adminMapper.selectByPrimaryKey(accountId).getName();
        log("add", adminName, contractDTO.getName());

        // add contract
        Long contractId = ConstantUtil.generateId();
        Contract contract = new Contract();
        BeanUtils.copyProperties(contractDTO, contract);
        contract.setCid(contractId);
        contractMapper.insert(contract);
        return contract;
    }

    @Override
    public Contract modifyContract(ContractDTO contractDTO, String accountId) {
        // log
        String adminName = adminMapper.selectByPrimaryKey(accountId).getName();
        log("modify", adminName, contractDTO.getName());

        // modify contract
        Contract contract = contractMapper.selectByPrimaryKey(contractDTO.getCid());
        BeanUtils.copyProperties(contractDTO, contract);
        contractMapper.updateByPrimaryKey(contract);
        return contract;
    }

    @Override
    public Contract deleteContract(ContractDTO contractDTO, String accountId) {
        // log
        String adminName = adminMapper.selectByPrimaryKey(accountId).getName();
        log("delete", adminName, contractDTO.getName());

        // delete contract: set "deleted" to 1
        Contract contract = contractMapper.selectByPrimaryKey(contractDTO.getCid());
        contract.setDeleted(1);
        contractMapper.updateByPrimaryKey(contract);
        return contract;
    }

    @Override
    public Object getList(Integer pageIndex) {
        Iterator<Contract> iterator = contractMapper.getList(pageIndex).iterator();
        Integer count = contractMapper.getCount();
        List<Map<String, Object>> list = new LinkedList();
        while (iterator.hasNext()) {
            Contract contract = iterator.next();
            Map<String, Object> map = new HashMap<>();
            String name = contract.getName();
            map.put("cid", contract.getCid());
            map.put("name", name);
            map.put("part_A", contract.getPart_A());
            map.put("part_B", contract.getPart_B());
            map.put("flag", contract.getFlag());
            map.put("money", contract.getMoney());
            map.put("account",accountMapper.getAccountInfo(contract.getPart_A()));
            map.put("count", count);
            list.add(map);
        }
        return list;
    }

    // 合同操作日志记录
    public void log(String action, String adminName, String contractName) {
        String logInfo = "%s contract(%s)";
        Log log = new Log();
        log.setName(adminName);
        log.setInfo(String.format(logInfo, action, contractName));
        logMapper.insertSelective(log);
    }

    @Override
    public Contract getContractDetail(Long cid) {
        return contractMapper.selectByPrimaryKey(cid);
    }

    @Override
    public void setFlag(Long cid, Integer flag, String accountId) {
        //将flag更改
        Contract contract = contractMapper.selectByPrimaryKey(cid);
        if (contract == null) {
            throw new BizException(ConstantUtil.BizExceptionCause.INVALID_CID);
        }
        contract.setFlag(flag);
        contractMapper.updateByPrimaryKey(contract);
    }

    @Override
    public void addSignature(Long cid, String aid, String url, String identify) {
        Contract contract = contractMapper.selectByPrimaryKey(cid);
        // 更新身份证号
        Account account = accountMapper.selectByPrimaryKey(contract.getPart_A());
        account.setIdentify(identify);
        accountMapper.updateByPrimaryKey(account);

        // 保存签名、更新签约时间
        contract.setSignatureUrl(url);
        Date signTime = new Date();
        contract.setSignTime(signTime);
        contractMapper.updateByPrimaryKey(contract);
    }
}
